package com.joker.aigc.tools.controller;

import com.joker.aigc.domain.entity.R;
import com.joker.aigc.tools.domian.dto.SqlConvertDTO;
import com.joker.aigc.tools.service.SqlConvertService;
import com.joker.aigc.tools.utils.FileUtils;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;



/**
 * @Desciption:
 * @Author: feixiang.li
 * @date: 2024-08-05 12:32
 **/
@Validated
@RestController
@RequestMapping("/api/1.0/tools")
public class SqlConvertController {

    @Autowired
    SqlConvertService sqlConvertService;

    /**
     * 将sql转换为markdown
     *
     * @param sqlConvertDTO sql
     * @return mardown字符串
     */
    @PostMapping("/sql/convertSqlToMarkdown")
    public R<String> convertSqlToMarkdown(@RequestBody @Valid SqlConvertDTO sqlConvertDTO) {
        return R.ok(sqlConvertService.convertSqlToMarkdown(sqlConvertDTO.getSql()));
    }

    /**
     * 将sql文件转换为markdown
     * @param multipartFile
     * @return
     */
    @PostMapping(value = "/sql/convertFileSqlToMarkdown", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public R<String> convertFileSqlToMarkdown(@RequestParam("file") MultipartFile multipartFile) {
        String sql = FileUtils.readFile(multipartFile);
        return R.ok(sqlConvertService.convertSqlToMarkdown(sql));
    }

    @PostMapping("/sql/convertDTOToMarkdown")
    public R<String> convertDTOToMarkdown(@RequestBody @Valid SqlConvertDTO sqlConvertDTO) {
        return R.ok(sqlConvertService.convertDtoToMarkdown(sqlConvertDTO.getSql()));
    }


    /**
     * 比较两个sql文件
     * @param multipartFile1
     * @param multipartFile2
     * @return
     */
    @PostMapping(value = "/sql/compareFileSqlToMarkdown", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public R<String> compareFileSqlToMarkdown(@RequestParam("file1") MultipartFile multipartFile1,
                                              @RequestParam("file2") MultipartFile multipartFile2) {
        return R.ok(sqlConvertService.getCreateTableSql(multipartFile1, multipartFile2));
    }


}
